<?php
namespace app\index\controller;

use think\Controller;

class Index extends Controller
{
    public function index()
    {
        //获取某个分类下的文章
        $category_alias = $this->request->param('category_alias');
        $article_category_id = model('Category')->getFieldByAlias($category_alias, 'id');
        $assign['category_id'] = $article_category_id;
        $assign['category_name'] = model('Category')->getFieldById($article_category_id, 'name');
        if ($article_category_id) {
            $where[] = ['category_id','=',$article_category_id];
        }
        $where[] = ['is_publish','=',1];
        $article_list = model('Listing')->where($where)->order('id DESC')->paginate();
        $assign['page'] = $article_list->render();
        $assign['total'] = $article_list->total();
        $list = $article_list->toArray()['data'];
        //循环文章列表，获取要显示的具体内容
        if (is_array($list) && count($list)) {
            foreach($list as $k=>$v) {
                if ($v['is_doc']) {
                    $return = model('Doc')->find($v['rel_id'])->toArray();
//                    $return['category_name'] = model('Category')->getFieldById($return['category_id'],'name');
                    $return['article_list'] = model('Article')->where([['doc_id','=',$v['rel_id']],['is_publish','=',1]])->limit(12)->select()->toArray();
                }
                else {
                    $return = model('Article')->find($v['rel_id'])->toArray();
//                    $return['category_name'] = model('Category')->getFieldById($return['category_id'],'name');
                }
                $return['is_doc'] = $v['is_doc'];
                $return['title'] = isset( $return['title'] ) ? $return['title'] : $return['name'];
                $category_info = model('Category')->where('id', $return['category_id'])->find();
                $return['category_name'] = $category_info['name'];
                $return['category_alias'] = $category_info['alias'];
                $assign['article_list'][] = $return;
            }
        }

        $this->assign($assign);
        return $this->fetch();
    }

    //搜索页
    public function search()
    {
        return $this->fetch();
    }

    //文章页
    public function content()
    {
        $id = $this->request->param('id');
        //更新阅读数
        model('Article')->where('id', $id)->setInc('view_count');

        //获取所有顶级分类
        $assign['categories'] = model('Category')->where("pid", 0)->select()->toArray();
        $assign['content'] = model('Article')->where('id', $id)->find();
        $category_info = model('Category')->where('id', $assign['content']['category_id'])->find();
        $assign['content']['category_name'] = $category_info['name'];
        $assign['content']['category_alias'] = $category_info['alias'];
        $this->assign($assign);
        return $this->fetch();
    }

    //文档页
    public function doc()
    {
        //获取文档ID
        $id = $this->request->param('id');
        //更新阅读数
        model('Doc')->where('id', $id)->setInc('view_count');
        //获取文档信息
        $assign['doc'] = model('Doc')->where('id',$id)->find();
        //获取文章ID
        $assign['article_id'] = $this->request->param('article_id');
        //获取所有文章列表
        $where[] = ['doc_id','=',$id];
        $where[] = ['is_publish','=',1];
        $assign['title_list'] = model('Article')->where($where)->field('id,doc_id,title')->order(['sort'=>'desc','id'=>'asc'])->select();
        if (!$assign['article_id']) {
            $assign['article_id'] = $assign['title_list'][0]['id'];
        }
        //更新阅读数
        model('Article')->where('id', $assign['article_id'])->setInc('view_count');
        $assign['content'] = model('Article')->getFieldById($assign['article_id'],'content');
        //获取文章内容
        $this->assign($assign);
        return $this->fetch();
    }


//------------------------------------------------------------------


//    public function index_static()
//    {
//        return $this->fetch();
//    }
//
//    public function category_static()
//    {
//        return $this->fetch();
//    }
//
//    public function content_static()
//    {
//        return $this->fetch();
//    }
//
//    public function search_static()
//    {
//        return $this->fetch();
//    }
//
    public function doc_static()
    {
        return $this->fetch();
    }
}
